The last few sessions have seen us printing to the screen like maniacs.
We have printed variables, literals and a few different types.
print("hello")
name = "Chris"
print(name)
When I introduced strings, we saw there are a few different ways to write them, the same is true for formatting a string.
Just printing a variable to the screen is a terrible idea, especially if you are using print statements to debug your code
In [2]:
name = "bob"
print(name)
name * 5
print(name)
print(name * 10) #This output is kinda useless, right?
In [4]:
name = 11
print("Name is equal to " + str(name))
print("Something about: ")
print(name)
name *= 5
print("Name has been multiplied by 5 and is now equal to " + name) #slightly more informative
def method_name(argument0, argument1):
variable_inside_method = "I am indented, so I am part of the function"
return variable_inside_method
def
is the keyword used to define the start of a declarationreturn
a value, but we do not have to!
In [4]:
def return_a_sum(arg0, arg1):
return arg0 + arg1
returned_variable = return_a_sum(40, 2) #call the function here and return something
print(returned_variable)
Methods are functions that are attached to objects. While we call a function like this:
function(argument1, argument2)
Methods are called like this:
object.method(argument1, argument2)
Objects in Python have methods attached to them that you can use. Let's learn about methods attached to strings!
What is the name of the function
in Python that lists the methods
attached to an object?
In [2]:
print("My name is {}".format('Fred'))
print("My name is {} {}".format(4, 'Terry'))
print("My name is {0}".format(True)) # format does not care about types
Many languages start at a zero index, this means that when you have 10 elements, their index
ranges from 0 to 9. (We will see this in more detail when we come to lists
in Python)
INDEX: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
ELEMENT: "a"|"b"|"c"|"d"|"e"|"f"|"g"|"h"|"i"|"j"|
In [ ]:
`"Never {0} give you {1}, Never {0} let you {2}".format("gonna", "up", "down")`
We can reuse arguments by referring to their index here!
In [ ]:
my_string = "Super Cool Words"
print(len(my_string))
print("super cool {0}".format("words"))
print(my_string.lower())
print(type(my_string))
print(my_string.upper())
print(int(my_string))
In [8]:
pet_name = "Mr. Woofington"
print("Hello " + pet_name)
pet_name = 14055
print("Hello " + pet_name) #Why is this?
In [ ]:
print(type(1))
print(type("what am I?"))
print(type(True))
In [ ]:
print(str(1)) #convert int to string
print(int(True)) #bool to int
print(int("12")) #string to int
Write a script that:
In [1]:
global_var = "I am a global variable"
def a_function():
local_var = "I am local"
print(local_var)
a_function()
print(global_var)
print(local_var) # What happens here?
In [ ]:
def func():
local = "I am local"
return(local)
global_var = func()
print(global_var)
In [9]:
age = 200
def change_age():
age = "test"
print(age)
change_age()
print(age) #What should this equal?
In [10]:
age = 200
def change_age():
global age
age = "test"
print(age)
change_age()
print(age)
def change_age_no_glob(age):
age = "test"
return age
age = change_age_no_glob(age)
In [7]:
def function_to_do_stuff():
g_var = "Hello"
function_to_do_stuff()
print(g_var)
In [8]:
def function_to_do_stuff():
global g_var # makes new variable available in global scope
g_var = "Hello"
function_to_do_stuff()
print(g_var)